package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        //传入当前页,以及每页显示条数
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Customer> list = customerMapper.selectForList(qo);
        return new PageInfo(list);
    }
    @Override
    public void save(Customer customer) {
        //插入销售员的id和录入人员的id
        Subject subject = SecurityUtils.getSubject();
        Employee currentEmployee = (Employee) subject.getPrincipal();
        customer.setSeller(currentEmployee);
        customer.setInputUser(currentEmployee);
        //录入时间
        customer.setInputTime(new Date());
        customerMapper.insert(customer);
    }
    @Override
    public void update(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }
    @Override
    public Customer get(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Customer> list() {
        return customerMapper.selectAll();
    }

    @Override
    public void updateStatus(Long cid, Integer status) {
        if(status == Customer.status_formal){
            customerMapper.updateStatus(cid,status,new Date());
        }else{
            customerMapper.updateStatus(cid,status,null);
        }
    }

    @Override
    public List<Customer> queryAllFormalCustomer(Integer statusFormal) {
        return customerMapper.queryAllFormalCustomer(statusFormal);
    }
}
